package com.cumt.threeway.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.cumt.threeway.R;
import com.cumt.threeway.constant.URLS;
import com.cumt.threeway.model.Single;
import com.cumt.threeway.table.ActivitiesEntity;
import com.cumt.threeway.util.ThreeWayData;
import com.cumt.threeway.util.ThreeWayUtil;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by 胡晶晶 on 2016/5/23.
 */
public class SubmitInfoActivity extends FragmentActivity implements View.OnClickListener{

    private TextView mTvTitle;
    private TextView mTvSite;
    private TextView mTvSubmitDate;
    private EditText mEtName;
    private EditText mEtPhone;
    private Single mSingle;
    private ActivitiesEntity mActivitiesEntity;
    private RequestQueue queue;

    private Handler mHanddler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify_baoming_yuding);
        initView();
    }

    private void initView() {
        queue = Volley.newRequestQueue(this);
        mActivitiesEntity = (ActivitiesEntity) getIntent().getSerializableExtra("single");
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mEtName = (EditText) findViewById(R.id.et_name);
        mEtPhone = (EditText) findViewById(R.id.et_shouji);
        mTvSite = (TextView) findViewById(R.id.tv_dizhi);
        mTvSubmitDate = (TextView) findViewById(R.id.tv_riqi);

        mTvTitle.setText(mActivitiesEntity.getName());
        mTvSite.setText(mActivitiesEntity.getSite());
        mTvSubmitDate.setText(mActivitiesEntity.getTime());
        findViewById(R.id.btn_back).setOnClickListener(this);
        findViewById(R.id.tv_submit).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.tv_submit:
                if(TextUtils.isEmpty(getEditContent(mEtName)) || TextUtils.isEmpty(getEditContent(mEtPhone))) {
                    ThreeWayUtil.getInstance().showToast(SubmitInfoActivity.this, getResources().getString(R.string.submit_info_error));
                } else if(!ThreeWayUtil.getInstance().isPhoneNum(getEditContent(mEtPhone))){
                    ThreeWayUtil.getInstance().showToast(this, getResources().getString(R.string.tip_phone_error));
                } else {
//                    submitInfoToServer();
                    ThreeWayData.getInstance().updateMySubmitActivitiesInfo(true, mActivitiesEntity.getName(), getEditContent(mEtName), getEditContent(mEtPhone));
                    goCheckSubmitInfoActivity();
                }
                break;
        }
    }

    private void submitInfoToServer() {
        StringRequest srPost = new StringRequest(Request.Method.POST,
                URLS.IP + "NetServlet/SingleSubmitData",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        String dingdan = response.toString();
                        if (dingdan.equals("true\r\n")) {
                            goCheckSubmitInfoActivity();
                        } else {
                            ThreeWayUtil.getInstance().showToast(SubmitInfoActivity.this, getResources().getString(R.string.net_error));
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {
            // 当手机发送请求时，回调用getParams方法，拼请求
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("username1", getEditContent(mEtName));
                map.put("telephone1", getEditContent(mEtPhone));
                map.put("shijian1", mSingle.getDate());
                map.put("address",mSingle.getAcontent());
                map.put("title1", mSingle.getAname());
                map.put("loginname",ThreeWayUtil.getInstance().getLoginAccount(SubmitInfoActivity.this));
                return map;
            }
        };
        queue.add(srPost);
    }

    private void goCheckSubmitInfoActivity() {
        ThreeWayUtil.getInstance().showToast(SubmitInfoActivity.this, getResources().getString(R.string.submit_info_success));
        mHanddler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
                Intent intent = new Intent(SubmitInfoActivity.this, CheckSubmitActivity.class);
                startActivity(intent);
            }
        }, 600);
    }

    private String getEditContent(EditText editText) {
        return editText.getText().toString();
    }
}
